# -*- coding:utf-8 -*-

import datetime, time, socket, traceback
import logging, logging.handlers, base64

K_ERR_SRV               = '服务器错误'
K_ERR_PARAMS            = '参数错误'
K_ERR_USER_DISABLE      = '用户被禁用'
K_ERR_USER_UNEXIST     	= '用户不存在'
K_ERR_USER_LOST         = '该卡已挂失'
K_ERR_CODE_FAIL			= '获取验证码失败'
K_ERR_CODE_WRONG		= '验证码错误'
K_ERR_MSG				= '请求失败'
K_ERR_START				= '开启失败'
K_ERR_STOP              = '停止失败'
K_ERR_DEV_UNEXIST		= '设备不存在'
K_ERR_PRINT_LIMIT       = '打印次数已达到上限'

handler = logging.handlers.RotatingFileHandler('logs/water.log', maxBytes = 50*1024*1024, backupCount = 10)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s - %(message)s')
handler.setFormatter(formatter)
rctLogger = logging.getLogger('water')
rctLogger.addHandler(handler)
rctLogger.setLevel(logging.DEBUG)
stream = logging.StreamHandler()
stream.setLevel(logging.INFO)
stream.setFormatter(formatter)
rctLogger.addHandler(stream)

def isEmpty(aVal):
    if(not aVal) or (len(aVal) <= 0):
        return True
    return False

def timeUnix():
	return time.mktime(datetime.datetime.now().timetuple())
	
def timeDate():
	return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

def hthex2str(aVal) :
	tmpStr = ''
	if aVal and len(aVal) > 0 :
		for c in aVal :
			tmpStr += "%02X" % ord(c)
	return tmpStr

def htprint(aFlag, aVal):
	if not aVal or len(aVal) <= 0 :
		print "%s-len=0,data="
		
	print "%s-len=%d,data=" % (aFlag,len(aVal)),
	for c in aVal:
		print hex(ord(c)),
	print

def cmdSend(aIp, aPort, aCmd):
    try:
        s = socket.socket()
        s.connect((aIp,aPort))
        s.sendall(aCmd)
        recv = s.recv(1024*10)
        s.close()
        return recv
    except socket.gaierror, e:
        print e
    except socket.error, e:
        print e
    return 9

def splitRsp(aStr):
    if aStr == "<{true}[||]>" or aStr == "<{false}>":
        return '-1'
    tmpRet =aStr.split("|")[1]
    return tmpRet

def base64Decode(aData):
    if (not aData or len(aData) <= 0) :
        return None
    tmpData = 4 - len(aData) % 4
    if tmpData :
        aData += b'='* tmpData
    return base64.b64decode(aData)

def base64Encode(aData):
    if (not aData or len(aData) <= 0) :
        return None
    return base64.b64encode(aData)